package com.junlong0716.enjoylearning.adapter

import android.view.View
import android.widget.CheckBox
import android.widget.SectionIndexer
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.htxcsoft.corelibrary.glide.GlideUtils
import com.junlong0716.enjoylearning.R
import com.junlong0716.enjoylearning.entity.SortModel
import java.util.*

/**
 *@author: 巴黎没有摩天轮Li
 *@description:
 *@date: Created in 下午9:28 2018/7/16
 *@modified by:
 */
class ContactAdapter(data: ArrayList<SortModel>, resLayout: Int) : BaseQuickAdapter<SortModel, BaseViewHolder>(resLayout, data), SectionIndexer {

    private var mList: List<SortModel>? = null
    private val mSelectedList: MutableList<SortModel>

    init {
        mSelectedList = ArrayList()
        if (data == null) {
            this.mList = ArrayList()
        } else {
            this.mList = data
        }
    }

    override fun getSections(): Array<Any>? {
        return null
    }

    override fun getSectionForPosition(position: Int): Int {
        return data[position].sortLetters!![0].toInt()
    }

    override fun getPositionForSection(sectionIndex: Int): Int {
        for (i in 0 until data.size) {
            val sortStr = data[i].sortLetters
            val firstChar = sortStr!!.toUpperCase(Locale.CHINESE)[0]
            if (firstChar.toInt() == sectionIndex) {
                return i
            }
        }
        return -1
    }

    override fun convert(helper: BaseViewHolder?, item: SortModel?) {
//        val cb = helper!!.getView<CheckBox>(R.id.cb_check)
//        cb.isChecked = item!!.isChecked
        helper!!.setText(R.id.tv_user_name, item!!.name)
        GlideUtils.showImage(mContext, item.avatar, helper.getView(R.id.civ_avatar), R.mipmap.ic_avatar_man)
//        //helper.setText(R.id.)

        //根据position获取分类的首字母的Char ascii值
        val section = getSectionForPosition(helper.adapterPosition)


        //如果当前位置等于该分类首字母的Char的位置 ，则认为是第一次出现
        if (helper.adapterPosition == getPositionForSection(section)) {
            helper.setVisible(R.id.catalog, true)
            helper.setText(R.id.catalog, item.sortLetters)
        } else {
            // helper.setVisible(R.id.catalog,false)
            helper.getView<TextView>(R.id.catalog).visibility = View.GONE
        }

        if (mList!![helper.adapterPosition].isSearch){

            helper.getView<TextView>(R.id.catalog).visibility = View.GONE
        }

        helper.setTag(R.id.cb_check,helper.adapterPosition)

        helper.getView<CheckBox>(R.id.cb_check).isChecked = isSelected(mList!![helper.adapterPosition])
    }


    override fun getItemCount(): Int {
        return mList!!.size
    }


    override fun getItem(position: Int): SortModel? {
        return mList!![position]
    }

    /**
     * 当ListView数据发生变化时,调用此方法来更新ListView
     * @param list
     */
    fun updateListView(list: List<SortModel>?) {
        if (list == null) {
            this.mList = ArrayList()
        } else {
            this.mList = list
        }
        notifyDataSetChanged()
    }


    private fun isSelected(model: SortModel): Boolean {
        return mSelectedList.contains(model)
        //return true;
    }

    fun toggleChecked(position: Int) {
        if (isSelected(mList!![position])) {
            removeSelected(position)
        } else {
            setSelected(position)
        }
    }

    fun getSelectList(): MutableList<SortModel> {
        return mSelectedList
    }

    private fun setSelected(position: Int) {
        if (!mSelectedList.contains(mList!![position])) {
            mSelectedList.add(mList!![position])
        }
    }

    private fun removeSelected(position: Int) {
        if (mSelectedList.contains(mList!![position])) {
            mSelectedList.remove(mList!![position])
        }
    }

    fun clearSelected(){
        if (mSelectedList.isEmpty()){
            return
        }
        mSelectedList.clear()
    }
}